//----------------------
$(function(){
	
	$("#codebutton").click(function(){
		var action = $(this).attr("data-action");
		regist.sendButt = $(this);
		var $username = $("#username");
		var username = $username.val().trim() || "";
		if(!username){
			layer.msg('请输入手机号码');
			$username.focus();
			return false;
		}
		
		if(!/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(username)){
			layer.msg('手机号码格式输入有误');
			$username.focus();
			return false;
		}
		
		
		layer.open({
			  title:'请输入图形验证码',
			  type: 1,
			  area: ['90%', '28%'], //宽高
			  content: '<div class="inputbg inputcode" style="padding:10px 10px 10px 10px;"><label class="labelbox" for=""><input class="code" type="text" name="captchaCode" id="captchaCode" autocomplete="off" placeholder="图片验证码"></label><img alt="图片验证码" title="看不清换一张" class="icode_image code-image chkcode_img" src="/kaptcha.htm" onclick="this.src=&quot;/kaptcha.htm?r=&quot;+Math.random()" ></div>        <div class="mar_phone_dis1"><input class="btn33280 btn_reg_3" id="registCaptcha"  type="button" value="确认"></div>'
			  
		});
		
		
		//提交验证码
		$("#registCaptcha").click(function(){
			
			var $captchaCode = $("#captchaCode");
			var captchaCode = $captchaCode.val().trim() || "";
			if(!captchaCode){
				layer.msg('请输入图形验证码');
				$captchaCode.focus();
				return false;
			}
			
			if(!/[0-9]{3}/g.test(captchaCode)){
				layer.msg('图形验证码输入有误');
				$captchaCode.val("");
				$captchaCode.focus();
				return false;
			}
			
			$.ajax({
				url: "/"+action+"/sendsms/"+username+"/"+captchaCode+".htm",
				type: "get",
				success: function(e) {
					layer.closeAll();
					layer.msg(e.message);
					if(e.code=='success'){
						regist.startTime();
					}
				},error: function(e) {
					layer.msg("网络遇到了一点问题,请稍后再试",{icon: 2,time: 2000}); 
				}
			})
		});
	})
	//注册
	$("#registerSubmit").click(function(){
		var $username=$("#username"),$mscode=$("#mscode"),$password=$("#password");
		var username=$("#username").val().trim() || "",
			mscode = $mscode.val().trim() || "",
		    password = $password.val().trim() || "";
		
		if(!username){
			layer.msg('请输入手机号码');
			$username.focus();
			return false;
		}
		
		if(!/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(username)){
			layer.msg('手机号码格式输入有误');
			$username.focus();
			return false;
		}
		
		if(!mscode){
			layer.msg("请输入短信验证码");
			$mscode.focus();
			return false;
		}
		
		if(!/[0-9]{6}/g.test(mscode)){
			layer.msg("短信验证码输入有误");
			$mscode.val("");
			$mscode.focus();
			return false;
		}
		
		if(!password){
			layer.msg("请输入登陆密码");
			$password.focus();
			return false;
		}
		
		if (!/^([0-9a-zA-Z]|[\_]){6,16}$/.test(password)) {
			layer.msg("登录密码必须为6-16位字母数字或下划线");
			$password.focus();
			return false;
		}
		
		if(!$("#agreement").is(":checked")){
			layer.msg("请阅读并勾选同意用户协议才能继续注册");
			return false;
		}
		
		var $registerSubmit = $(this);
		$registerSubmit.attr("disabled",true);
		$registerSubmit.addClass("button-disbale");
		$registerSubmit.val("正在提交...");
		$.ajax({
			url: "/user/registed.htm",
			type: "post",
			data:"username="+username+"&password="+password+"&mscode="+mscode,
			success: function(e) {
				if(e.code=='success'){
					
					layer.msg('恭喜注册成功！', {
					  time: 2000 
					}, function(){
						location.href="/user/login.htm";
					});   
					
				}else{
					layer.msg(e.message);
					$registerSubmit.attr("disabled",false);
					$registerSubmit.removeClass("button-disbale");
					$registerSubmit.val("重新提交");
				}
			},error: function(e) {
				layer.msg("网络遇到了一点问题,请稍后再试",{icon: 2,time: 2000}); 
			}
		})
		
		return false;
	});
	
	$("#loginForm").submit(function(){
		
		$('#kaptchaImage').attr('src','/kaptcha.htm?' + Math.floor(Math.random() * 100));
		
		var $username = $("#username"),$password=$("#password"),$loginSubmit =$("#loginSubmit"), backUrl = $("#backUrl").val().trim() || "",
		    username=$("#username").val().trim() || "",
		    password = $password.val().trim() || "",
		    code = $("#code").val();
		
		
		if(!username){
			layer.msg('请输入手机号码');
			$username.focus();
			return false;
		}
		
		if(!/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(username)){
			layer.msg('手机号码格式输入有误');
			$username.focus();
			return false;
		}
		
		if(!password){
			layer.msg("请输入登陆密码");
			$password.focus();
			return false;
		}
		
		if (!/^([0-9a-zA-Z]|[\_]){6,16}$/.test(password)) {
			layer.msg("登陆密码输入错误");
			$password.focus();
			return false;
		}
		
		$loginSubmit.attr("disabled",true);
		$loginSubmit.addClass("button-disbale");
		$loginSubmit.val("正在登陆...");
		$.ajax({
			url: "/user/signin.htm",
			type: "POST",
			data:"username="+username+"&password="+password+"&code="+code+"&returnUrl=",
			success: function(e) {
				if(e.code=='success'){
					if(backUrl){
						location.href=backUrl;
					}else{
						location.href="/user/index.htm";
					}
					
				}else{
					
					if(e.needCode=='Y'){
						$("#logincode").show();
					}
					
					layer.msg(e.message);
					$loginSubmit.attr("disabled",false);
					$loginSubmit.removeClass("button-disbale");
					$loginSubmit.val("重新登陆");
				}
			},error: function(e) {
				layer.msg("网络遇到了一点问题,请稍后再试",{icon: 2,time: 2000}); 
			}
		})
		
		return false;
	})
	
	//重置密码验证
	$("#forgetPwdNext").click(function(){
		
		var $username=$("#username"),$mscode=$("#mscode"),
		    username=$("#username").val() || "",
			mscode = $mscode.val().trim() || "",
			$forgetPwdNext=$(this);
		
		if(!username){
			layer.msg('请输入手机号码');
			$username.focus();
			return false;
		}
		
		if(!/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(username)){
			layer.msg('手机号码格式输入有误');
			$username.focus();
			return false;
		}
		
		if(!mscode){
			layer.msg("请输入短信验证码");
			$mscode.focus();
			return false;
		}
		
		if(!/[0-9]{6}/g.test(mscode)){
			layer.msg("短信验证码输入有误");
			$mscode.val("");
			$mscode.focus();
			return false;
		}
		$forgetPwdNext.attr("disabled",true);
		$forgetPwdNext.addClass("button-disbale");
		$forgetPwdNext.val("正在验证...");
		$.ajax({
			url: "/checkSmscode/"+username+"/"+mscode+".htm",
			type: "GET",
			success: function(e) {
				if(e.code=="success"){
					$("#forgetPwd-verify").hide();
					$("#forgetPwd-reset").show();
				}else{
					layer.msg(e.message);
					$forgetPwdNext.attr("disabled",false);
					$forgetPwdNext.removeClass("button-disbale");
					$forgetPwdNext.val("下一步");
				}
			},error: function(e) {
				layer.msg("网络遇到了一点问题,请稍后再试",{icon: 2,time: 2000}); 
			}
		})
	})
	
	//重置密码验证
	$("#forgetPwdForm").submit(function(){
		var $username=$("#username"),$mscode=$("#mscode"),$newPassword=$("#newPassword"),$confirmPassword=$("#confirmPassword"),
	    username=$("#username").val() || "",
		mscode = $mscode.val().trim() || "",
		newPassword = $newPassword.val().trim() || "",
		confirmPassword = $confirmPassword.val().trim() || "",
		$resetPwdSubmit=$("#resetPwdSubmit");
	
		if(!username){
			layer.msg('请输入手机号码');
			$username.focus();
			return false;
		}
		
		if(!/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(username)){
			layer.msg('手机号码格式输入有误');
			$username.focus();
			return false;
		}
		
		if(!mscode){
			layer.msg("请输入短信验证码");
			$mscode.focus();
			return false;
		}
		
		if(!/[0-9]{6}/g.test(mscode)){
			layer.msg("短信验证码输入有误");
			$mscode.val("");
			$mscode.focus();
			return false;
		}
		
		if(!newPassword){
			layer.msg("请输入新密码");
			$newPassword.focus();
			return false;
		}
		
		if (!/^([0-9a-zA-Z]|[\_]){6,16}$/.test(newPassword)) {
			layer.msg("登录密码必须为6-16位字母数字或下划线");
			$newPassword.focus();
			return false;
		}
		
		if(!confirmPassword){
			layer.msg("请确认输入密码");
			$confirmPassword.focus();
			return false;
		}
		
		if(newPassword != confirmPassword){
			layer.msg("两次密码输入不一致");
			$confirmPassword.focus();
			return false;
		}
		
		$resetPwdSubmit.attr("disabled",true);
		$resetPwdSubmit.addClass("button-disbale");
		$resetPwdSubmit.val("正在提交...");
		$.ajax({
			url: "/user/forgetPwd/reset.htm",
			type: "POST",
			data:"username="+username+"&newPassword="+newPassword+"&mscode="+mscode,
			success: function(e) {
				if(e.code=="success"){
					layer.msg("密码重置成功！,2秒后自动返回登陆",{icon: 1,time: 2000}, function(){
						location.href="/user/login.htm";
					}); 
				}else{
					layer.msg(e.message, function(){
						location.reload();
					}); 
				}
			},error: function(e) {
				
				layer.msg("网络遇到了一点问题,请稍后再试",{icon: 2,time: 2000}); 
			}
		})
		
		return false;
	})
	
	$("#search").click(function(){
		
		
		var $searchInput = $("#search_input")
		
        isTrue = $searchInput.is(".on"); // 判断.search-hd是否是展开状态
	    if($searchInput.addClass('on').find('input').val() == ""){ // 在输入框没有文字时执行
	        if(isTrue){ // isTrue等于 true 移除on，false就添加on
	        	$searchInput.removeClass('on').find('input').blur();
	            $('.search-select ul').hide();
	        }else{
	        	$searchInput.addClass('on').find('input').focus();
	        }
	    }else{ //提交事件search-hd
	    	$searchInput.find('input').focus();
	        if(isTrue){
	            $("#searchForm").submit();
	        }else{
	
	        }
	    }
		
	})
	
	$("#agreementRegister").click(function(){
		
		layer.open({
			  type: 2,
			  title: '86wood用户协议',
			  shadeClose: true,
			  shade: 0.8,
			  area: ['750px', '90%'],
			  content: '/agreement/register.htm' 
			}); 
		
	})

})

var regist={
	countdown: 60,
	startTime: function() {
		if (regist.countdown <= 0) {
			regist.sendButt.attr("disabled",false);
			regist.sendButt.val("重新发送");
			regist.countdown = 60;
		} else {
			regist.sendButt.attr("disabled",true);
			regist.sendButt.val(regist.countdown + "秒");
			regist.countdown--;
			setTimeout(function() {
				regist.startTime()
			}, 1000)
		}
	}
}

function redirect(url) {
	location.href = url;
}
